<?php
	/**
	 * Class to detect which browser is currently accessing the page/site
	 * @author Rossoft
	 * @copyright Rossoft
	 */
	class browser 
	{
		/**
		 * @var string $name
		 */
		public static $name = NULL;
		
		/**
		 * @var string $version
		 */
		public static $version = NULL;
		
		/**
		 * @var $useragent
		 */
		public static $useragent = NULL;
		
		/**
		 * @var string $platform
		 */
		public static $platform;
		
		/**
		 * @var string aol
		 */
		public static $aol = FALSE;
		
		/**
		 * @var string browser
		 */
		public static $browsertype;
		
			
		/**
		 * Method to get the browser details from the USER_AGENT string in 
		 * the PHP superglobals
		 * @param void
		 * @return string property platform 
		 */
		static function getBrowserOS()
		{
			$win = eregi("win", self::$useragent);
			$linux = eregi("linux", self::$useragent);
			$mac = eregi("mac", self::$useragent);
			$os2 = eregi("OS/2", self::$useragent);
			$beos = eregi("BeOS", self::$useragent);
			
			//now do the check as to which matches and return it
			if($win)
			{
				self::$platform = "Windows";
			}
			elseif ($linux)
			{
				self::$platform = "Linux"; 
			}
			elseif ($mac)
			{
				self::$platform = "Macintosh"; 
			}
			elseif ($os2)
			{
				self::$platform = "OS/2"; 
			}
			elseif ($beos)
			{
				self::$platform = "BeOS"; 
			}
			return self::$platform;
		}
		
		/**
		 * Method to test for Opera
		 * @param void
		 * @return property $broswer
		 * @return property version
		 * @return bool false on failure
		 */
		static function isOpera()
		{
			// test for Opera		
			if (eregi("opera",self::$useragent))
			{
				$val = stristr(self::$useragent, "opera");
				if (eregi("/", $val)){
					$val = explode("/",$val);
					self::$browsertype = $val[0];
					$val = explode(" ",$val[1]);
					self::$version = $val[0];
				}else{
					$val = explode(" ",stristr($val,"opera"));
					self::$browsertype = $val[0];
					self::$version = $val[1];
				}
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for FireFox
		 * @param void
		 * @return bool false on failure
		 */ 
		static function isFirefox()
		{
			if(eregi("Firefox", self::$useragent))
			{
				self::$browsertype = "Firefox"; 
				$val = stristr(self::$useragent, "Firefox");
				$val = explode("/",$val);
				self::$version = $val[1];
				return true;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for Konquerer
		 * @param void
		 * @return prop $browser
		 * @return prop $version
		 * @return bool true on success
		 */
		static function isKonqueror()
		{
			if(eregi("Konqueror",self::$useragent))
			{
				$val = explode(" ",stristr(self::$useragent,"Konqueror"));
				$val = explode("/",$val[0]);
				self::$browsertype = $val[0];
				self::$version = str_replace(")","",$val[1]);
				return TRUE;
			}
			else {
				return FALSE;
			}
			
		}//end func
		
		/**
		 * Method to check for Internet Explorer v1
		 * @param void
		 * @return bool true on success
		 * @return prop $browsertype
		 * @return prop $version
		 */
		static function isIEv1()
		{
			if(eregi("microsoft internet explorer", self::$useragent))
			{
				self::$browsertype = "MSIE"; 
				self::$version = "1.0";
				$var = stristr(self::$useragent, "/");
				if (ereg("308|425|426|474|0b1", $var))
				{
					self::$version = "1.5";
				}
				return TRUE;
			}
			else {
				return FALSE;
			}
		}//end function
		
		/**
		 * Method to check for Internet Explorer later than v1
		 * @param void
		 * @return bool true on success
		 * @return prop $browsertype
		 * @return prop $version
		 */
		static function isMSIE()
		{
			if(eregi("msie", self::$useragent) && !eregi("opera",self::$useragent))
			{
				self::$browsertype = "MSIE"; 
				$val = explode(" ",stristr(self::$useragent,"msie"));
				self::$browsertype = $val[0];
				self::$version = $val[1];
				
				return TRUE;
			}
			else {
				return FALSE;
			}
		}//end function
		
		/**
		 * Method to check for Galeon
		 * @param void
		 * @return bool true on success
		 */
		static function isGaleon()
		{
			if(eregi("galeon",self::$useragent))
			{
				$val = explode(" ",stristr(self::$useragent,"galeon"));
				$val = explode("/",$val[0]);
				self::$browsertype = $val[0];
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}//end func
		
		/**
		 * Now we do the tests for browsers I can't test...
		 * If someone finds a bug, please report it ASAP to me please!
		 */
		
		/**
		 * Method to check for WebTV browser
		 * @param void
		 * @return bool true on success
		 * @return prop $browsertype
		 * @return prop $version
		 */
		static function isWebTV()
		{
			if(eregi("webtv",self::$useragent))
			{
				$val = explode("/",stristr(self::$useragent,"webtv"));
				self::$browsertype = $val[0];
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for BeOS's NetPositive
		 * @param void
		 * @return bool true on success
		 * @return prop $browsertype
		 * @return prop $version
		 */
		static function isNetPositive()
		{
			if(eregi("NetPositive", self::$useragent))
			{
				$val = explode("/",stristr(self::$useragent,"NetPositive"));
				self::$platform = "BeOS"; 
				self::$browsertype = $val[0];
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for MSPIE (Pocket IE)
		 * @param void
		 * @return bool true on success
		 */
		static function isMSPIE()
		{
			if(eregi("mspie",self::$useragent) || eregi("pocket", self::$useragent))
			{
				$val = explode(" ",stristr(self::$useragent,"mspie"));
				self::$browsertype = "MSPIE"; 
				self::$platform = "WindowsCE"; 
				if (eregi("mspie", self::$useragent))
					self::$version = $val[1];
				else {
					$val = explode("/",self::$useragent);
					self::$version = $val[1];
				}
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to test for iCab
		 * @param void
		 * @return bool true on success
		 */
		static function isIcab()
		{
			if(eregi("icab",self::$useragent))
			{
				$val = explode(" ",stristr(self::$useragent,"icab"));
				self::$browsertype = $val[0];
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to test for the OmniWeb Browser
		 * @param void
		 * @return bool True on success
		 */
		static function isOmniWeb()
		{
			if(eregi("omniweb",self::$useragent))
			{
				$val = explode("/",stristr(self::$useragent,"omniweb"));
				self::$browsertype = $val[0];
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for Phoenix Browser
		 * @param void
		 * @return bool true on success
		 */
		static function isPhoenix()
		{
			if(eregi("Phoenix", self::$useragent))
			{
				self::$browsertype = "Phoenix"; 
				$val = explode("/", stristr(self::$useragent,"Phoenix/"));
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for Firebird
		 * @param void
		 * @return bool true on success
		 */
		static function isFirebird()
		{
			if(eregi("firebird", self::$useragent))
			{
				self::$browsertype = "Firebird"; 
				$val = stristr(self::$useragent, "Firebird");
				$val = explode("/",$val);
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for Mozilla alpha/beta
		 * @param void
		 * @return bool true on success
		 */
		static function isMozAlphaBeta()
		{
			if(eregi("mozilla",self::$useragent) && 
			   eregi("rv:[0-9].[0-9][a-b]",self::$useragent) && 
			   !eregi("netscape",self::$useragent))
			
			{
				self::$browsertype = "Mozilla"; 
				$val = explode(" ",stristr(self::$useragent,"rv:"));
				eregi("rv:[0-9].[0-9][a-b]",self::$useragent,$val);
				self::$version = str_replace("rv:","",$val[0]);
				return TRUE;
			}
			else {
				return FALSE;
			}
		}//end function
	
		/**
		 * Method to check for Mozilla Stable versions
		 * @param void
		 * @return bool true on success
		 */
		static function isMozStable()
		{
			if(eregi("mozilla",self::$useragent) &&
			   eregi("rv:[0-9]\.[0-9]",self::$useragent) && 
			   !eregi("netscape",self::$useragent))
			{
				self::$browsertype = "Mozilla"; 
				$val = explode(" ",stristr(self::$useragent,"rv:"));
				eregi("rv:[0-9]\.[0-9]\.[0-9]",self::$useragent,$val);
				self::$version = str_replace("rv:","",$val[0]);
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Method to check for Lynx and Amaya
		 * @param void
		 * @return bool true on success
		 */
		static function isLynx()
		{
			if(eregi("libwww", self::$useragent))
			{
				if (eregi("amaya", self::$useragent))
				{
					$val = explode("/",stristr(self::$useragent,"amaya"));
					self::$browsertype = "Amaya"; 
					$val = explode(" ", $val[1]);
					self::$version = $val[0];
				} else {
					$val = explode("/",self::$useragent);
					self::$browsertype = "Lynx"; 
					self::$version = $val[1];
				}
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * method to check for safari browser
		 * @param void
		 * @return bool true on success
		 */
		static function isSafari()
		{
			if(eregi("safari", self::$useragent))
			{
				self::$browsertype = "Safari"; 
				self::$version = "";
				return TRUE;
			}
			else {
				return FALSE;
			}
		}
		
		/**
		 * Various tests for Netscrape
		 * @param void
		 * @return bool true on success
		 */
		static function isNetscape()
		{
			if(eregi("netscape",self::$useragent))
			{
				$val = explode(" ",stristr(self::$useragent,"netscape"));
				$val = explode("/",$val[0]);
				self::$browsertype = $val[0];
				self::$version = $val[1];
				return TRUE;
			}
			elseif(eregi("mozilla",self::$useragent) && 
					!eregi("rv:[0-9]\.[0-9]\.[0-9]",self::$useragent))
			{
				$val = explode(" ",stristr(self::$useragent,"mozilla"));
				$val = explode("/",$val[0]);
				self::$browsertype = "Netscape"; 
				self::$version = $val[1];
				return TRUE;
			}
			else {
				return FALSE;
			}
		}//end func
		
		/**
		 * Method to check for AOL connections
		 * @param void
		 * @return bool true on Success
		 */
		static function isAOL()
		{
			if (eregi("AOL", self::$useragent)){
				$var = stristr(self::$useragent, "AOL");
				$var = explode(" ", $var);
				self::$aol = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
				return TRUE;
			}
			else { 
				return FALSE;
			}
		}
		
		/**
		 * Method to tie them all up and output something useful
		 * @param void
		 * @return array
		 */
		static function GetBrowserInfo()
		{
			$agent = $_SERVER['HTTP_USER_AGENT'];
			//set the useragent property
			self::$useragent = $agent;
			
			self::getBrowserOS();
			self::isOpera();
			self::isFirefox();
			self::isKonqueror();
			self::isIEv1();
			self::isMSIE();
			self::isGaleon();
			self::isNetPositive();
			self::isMSPIE();
			self::isIcab();
			self::isOmniWeb();
			self::isPhoenix();
			self::isFirebird();
			self::isLynx();
			self::isSafari();
			//self::isMozAlphaBeta();
			//self::isMozStable();
			//self::isNetscape();
			self::isAOL();
			return array
			(
				 'type' => self::$browsertype, 
				 'version' => self::$version, 
				 'os' => self::$platform, 
	//			 'AOL' => self::$aol
			); 
		}
	}
?>